package com.dongchedi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongchedi.entity.ProductCategory;
import com.dongchedi.mapper.ProductCategoryMapper;
import com.dongchedi.service.ProductCategoryService;
import com.dongchedi.utils.constant.RedisConstantKey;
import com.dongchedi.utils.page.PageRequest;
import com.dongchedi.utils.page.PageResult;
import com.dongchedi.utils.page.PageUtil;
import com.dongchedi.vo.FirstCategoryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author leibidong
 * @description 商品分类实现类
 * @date 2021/6/26
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    ProductCategoryMapper productCategoryMapper;
    @Resource
    RedisTemplate<String,Object> redisTemplate;
    /*
     * @Author: leibidong
     * @Description: 根据一级类目查询前六件商品
     * @Date: 2021/6/26 19:57
     * @Param: [categoryName, firstCategoryLevel]
     * @Return: com.alibaba.fastjson.JSONArray
     **/
    @Override
    public JSONArray   firstCategory(String categoryName, Integer firstCategoryLevel) {
        String key =RedisConstantKey.getProductByFirstLevel(categoryName,firstCategoryLevel);
        if(redisTemplate.opsForValue().get(key)!=null){
            String temp=(String) redisTemplate.opsForValue().get(key);
            return JSON.parseArray(temp);
        }else {
            List<FirstCategoryVO> list = productCategoryMapper.firstQuery(categoryName, firstCategoryLevel);
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < list.size(); i++) {
                JSONObject node = new JSONObject();
                node.put("id", list.get(i).getId());
                node.put("name", list.get(i).getName());
                node.put("price", list.get(i).getPrice());
                node.put("averageCost", list.get(i).getAverageCost());
                node.put("recommendDescript", list.get(i).getRecommendDescript());
                node.put("picUrl", list.get(i).getPicUrl());
                jsonArray.add(node);
            }
            redisTemplate.opsForValue().set(key,jsonArray.toJSONString(),3600,TimeUnit.SECONDS);
            return jsonArray;
        }
    }
    /*
     * @Author: leibidong
     * @Description: 根据二级类目进行查询商品
     * @Date: 2021/6/26 19:57
     * @Param: [pageRequest, categoryName, secondCategoryLevel]
     * @Return: com.alibaba.fastjson.JSONArray
     **/
    @Override
    public JSONArray secondCategory(PageRequest pageRequest, String categoryName, Integer secondCategoryLevel) {
        PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize());
        List<FirstCategoryVO> list=productCategoryMapper.secondQuery(categoryName,secondCategoryLevel);
        PageResult pageResult= PageUtil.getPageResult(pageRequest,new PageInfo<FirstCategoryVO>(list));
        JSONArray jsonArray = new JSONArray();
        for(int i=0;i<pageResult.getContent().size();i++){
           FirstCategoryVO firstCategoryVO=(FirstCategoryVO) pageResult.getContent().get(i);
            JSONObject node = new JSONObject();
            node.put("id",firstCategoryVO.getId());
            node.put("name",firstCategoryVO.getName());
            node.put("price",firstCategoryVO.getPrice());
            node.put("averageCost",firstCategoryVO.getAverageCost());
            node.put("recommendDescript",firstCategoryVO.getRecommendDescript());
            node.put("picUrl",firstCategoryVO.getPicUrl());
            jsonArray.add(node);
        }
        return jsonArray;
    }
    /*
     * @Author: leibidong
     * @Description: 根据一级类目查询出对应的所有二级类目
     * @Date: 2021/6/26 19:58
     * @Param: [firstCategoryName]
     * @Return: com.alibaba.fastjson.JSONArray
     **/
    @Override
    public JSONArray firstSearchSecondName(String firstCategoryName) {
        String key= RedisConstantKey.getProductCategoryKey(firstCategoryName);
        if(redisTemplate.opsForValue().get(key)!=null){
            String temp=(String) redisTemplate.opsForValue().get(key);
            return JSON.parseArray(temp);
        }else {
            List<ProductCategory> list = productCategoryMapper.firstSearchSecondName(firstCategoryName);
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < list.size(); i++) {
                JSONObject node = new JSONObject();
                node.put("name", list.get(i).getName());
                node.put("code", list.get(i).getCode());
                node.put("level", list.get(i).getLevel());
                jsonArray.add(node);
            }
            redisTemplate.opsForValue().set(key,jsonArray.toJSONString(),3600, TimeUnit.SECONDS);
            return jsonArray;
        }
    }
}
